package com.sky.service.user.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.mapper.merchant.MerchantMapper;
import com.sky.mapper.user.OrderMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.Merchant;
import com.sky.pojo.Orders;
import com.sky.service.user.OrderService;
import com.sky.utils.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Author:篍
 * @Date: 2025/08/29 21:11
 * @Description:
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;



    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public LocalDateTime getEstimatedDeliveryTime(Long shopId, String customerAddress) {
        //预计送达时间 = 当前时间+菜品制作时间+配送时间+预留时间（10分钟）
        Merchant merchant = merchantMapper.selectById(shopId);
        String shopAddress = merchant.getShopAddress();
        //配送时间
        Map<String, String> distanceAndTimeByAddress = MapUtils.gaodeTwoPointCostDistanceAndTimeByAddress(shopAddress, customerAddress);
        long deliveryTime = Long.parseLong(distanceAndTimeByAddress.get("duration"));
        //菜品制作时间
        Long makeTime = shoppingCartMapper.getTotalMakeTimeByUserId(BaseContext.getCurrentId());
        return LocalDateTime.now().plusMinutes(makeTime).plusSeconds(deliveryTime).plusMinutes(10);
    }
}
